<template>
    <div class='container'>
        <el-form :inline='true' :model='dataForm' @keyup.enter.native='getDataList()'>
            <el-form-item>
                <el-input v-model='dataForm.paramKey' placeholder='姓名' clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click='getDataList()'>查询</el-button>
                <el-button type='primary' @click='addOrUpdateHandle()'>新增</el-button>
                <el-button type='success' @click='exportStudents()'>导出 Excel</el-button>
                <el-button type='danger' @click='deleteHandle()' :disabled='dataListSelections.length <= 0'>批量删除
                </el-button>
            </el-form-item>
        </el-form>
        <div class='marking'>
            标记：
                        <el-tag class='item' type='info' closable v-for='item in tagList' @close='deleteTag(item.id)'>
                            <span class='dot' :style='{ backgroundColor: item.color }'></span>
                            <span :style='{ color: item.color }'> {{ item.name }}</span>
                        </el-tag>
            <el-button type='primary' icon='el-icon-plus' size='mini' @click='dialogVisible=true'></el-button>
        </div>
        <el-table
            :data='dataList'
            border
            v-loading='dataListLoading'
            @selection-change='selectionChangeHandle'
            style='width: 100%;'>
            <el-table-column
                type='selection'
                header-align='center'
                align='center'
                width='50'>
            </el-table-column>
            <el-table-column
                prop='id'
                header-align='center'
                align='center'
                width='80'
                label='ID'>
            </el-table-column>
            <el-table-column
                prop='stuNo'
                header-align='center'
                align='center'
                label='学号'>
            </el-table-column>
            <el-table-column
                prop='name'
                header-align='center'
                align='center'
                label='姓名'>
            </el-table-column>
            <el-table-column
                prop='phone'
                header-align='center'
                align='center'
                label='电话'>
            </el-table-column>
            <el-table-column
                prop='clazz'
                header-align='center'
                align='center'
                label='班级'>
            </el-table-column>
            <el-table-column
                prop='registrationTime'
                header-align='center'
                align='center'
                label='报名时间'>
            </el-table-column>
            <el-table-column
                header-align='center'
                align='center'
                label='标记'>
                <template slot-scope='scope'>
                    <div v-if='scope.row.tagId !== -1' @click='updateStuTag(scope.row)'>
                        <span class='dot' :style='{ backgroundColor: scope.row.tags.color }'></span>
                    </div>
                    <div v-else @click='updateStuTag(scope.row)'>
                        点击添加标记
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                fixed='right'
                header-align='center'
                align='center'
                width='150'
                label='操作'>
                <template slot-scope='scope'>
                    <el-button type='text' size='small' @click='addOrUpdateHandle(scope.row.id)'>修改</el-button>
                    <el-button type='text' size='small' @click='deleteHandle(scope.row.id)'>删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            style='margin-top: 10px'
            @size-change='sizeChangeHandle'
            @current-change='currentChangeHandle'
            :current-page='pageIndex'
            :page-sizes='[10, 20, 50, 100]'
            :page-size='pageSize'
            :total='totalPage'
            layout='total, sizes, prev, pager, next, jumper'>
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if='addOrUpdateVisible' ref='addOrUpdate' @refreshDataList='getDataList'></add-or-update>
        <el-dialog
            title='添加'
            :visible.sync='dialogVisible'
            width='30%'>
            <el-form>
                <el-color-picker v-model='tags.color'></el-color-picker>
                <el-input placeholder='标签名称' v-model='tags.name' />
            </el-form>
            <span slot='footer' class='dialog-footer'>
    <el-button @click='dialogVisible = false'>取 消</el-button>
    <el-button type='primary' @click='saveTags()'>确 定</el-button>
  </span>
        </el-dialog>
        <el-dialog
            title='添加'
            :visible.sync='dialogVisible2'
            width='30%'>
            <div class='marking'>
                <el-tag class='item' type='info' v-for='item in tagList' @click='selectTag(item.id)'
                        style='cursor: pointer'>
                    <span class='dot' :style='{ backgroundColor: item.color }'></span>
                    <span :style='{ color: item.color }'> {{ item.name }}</span>
                </el-tag>
                <el-tag class='item' type='info' @click='selectTag(-1)'>
                    <span>无标签</span>
                </el-tag>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import AddOrUpdate from './student-add-or-update';
import apis from '@/api/apis';
import { saveAs } from 'file-saver';

export default {
    data() {
        return {
            dataForm: {
                paramKey: ''
            },
            tags: {
                color: '#409EFF',
                name: ''
            },
            dataList: [],
            pageIndex: 1,
            pageSize: 10,
            totalPage: 0,
            dataListLoading: false,
            dialogVisible: false,
            dialogVisible2: false,
            dataListSelections: [],
            tagList: [],
            addOrUpdateVisible: false,
            row: {}
        };
    },
    components: {
        AddOrUpdate
    },
    created() {
        this.getDataList();
        this.getTags();
    },
    methods: {
        selectTag(id) {
            this.row.tagId = id;
            apis.saveEnrollmentInfo(this.row).then(res => {
                if (res && res.code === 0) {
                    this.getDataList();
                    this.dialogVisible2 = false;
                } else {
                    this.$message.error(res.msg);
                }
            });
        },
        updateStuTag(row) {
            //修改学生标记
            this.row = row;
            this.dialogVisible2 = true;
        },
        deleteTag(id) {
            apis.deleteTags([id]).then(res => {
                this.getTags();
            });
        },
        saveTags() {
            if (this.tags.name === '') {
                this.$message.error('请输入名称');
                return;
            }
            apis.saveTags(this.tags).then(res => {
                if (res.code === 0) {
                    this.getTags();
                } else {
                    this.$message.error(res.msg);
                }
            });
            this.dialogVisible = false;
        },
        getTags() {
            apis.getTagsData().then(res => {
                this.tagList = res.data;
            });
        },
        // 获取数据列表
        getDataList() {
            this.dataListLoading = true;
            var params = {
                'page': this.pageIndex,
                'limit': this.pageSize,
                'name': this.dataForm.paramKey
            };
            apis.getEnrollmentInfoData(params).then(data => {
                if (data && data.code === 0) {
                    this.dataList = data.page.list;
                    this.totalPage = data.page.totalCount;
                } else {
                    this.dataList = [];
                    this.totalPage = 0;
                }
                this.dataListLoading = false;
            });
        },
        // 每页数
        sizeChangeHandle(val) {
            this.pageSize = val;
            this.pageIndex = 1;
            this.getDataList();
        },
        // 当前页
        currentChangeHandle(val) {
            this.pageIndex = val;
            this.getDataList();
        },
        // 多选
        selectionChangeHandle(val) {
            this.dataListSelections = val;
        },
        // 新增 / 修改
        addOrUpdateHandle(id) {
            this.addOrUpdateVisible = true;
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id);
            });
        },
        async exportStudents() {
            // 发送请求获取Excel流
            apis.exportStudents().then(res => {
                const blob = new Blob([res.data], {
                    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                });
                saveAs(blob, '学生数据.xlsx');
            });
        }
    },
    // 删除
    deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
            return item.id;
        });
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            apis.deleteEnrollmentInfo(ids).then(data => {
                if (data && data.code === 0) {
                    this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                            this.getDataList();
                        }
                    });
                } else {
                    this.$message.error(data.msg);
                }
            });
        }).catch(() => {
        });
    }
};
</script>
<style>
.marking {
    margin-bottom: 10px;
    border: 1px solid #eee;
    padding: 10px;
    color: #797979;
    display: flex;
    align-items: center;
}

.item {
    display: flex;
    align-items: center;
    font-size: 13px;
    margin-right: 15px;
}

.dot {
    display: inline-block;
    background: pink;
    width: 12px;
    height: 12px;
    margin-right: 5px;
    border-radius: 50%;
}
</style>
